import * as THREE from 'three';
import * as Util from "./utils";
import {General, createGeneral} from "../js/general";
import R from "./resource";


export class World extends General {
    constructor(id) {
        super(id);

    }

    create () {
        this.camera = new THREE.PerspectiveCamera( 50, this.SCREEN_WIDTH / this.SCREEN_HEIGHT, 0.1, 1000 );
        this.camera.position.y = 100;
        // console.log(this.camera.position);
        
    }
    created () {
        this.demo();
    }

    demo () {
        let textureCube = new THREE.CubeTextureLoader().load( R.SKY );
        this.scene.background = textureCube;
        // ★★设置相机初始视角的正确方式
        let pos = new THREE.Vector3( 0, 0, -400 );
        this.camera.position.z = 200;
        this.camera.lookAt(pos);
        this.controls.target.set( pos.x, pos.y, pos.z );

        // this.controls.enabled = true;
        let ambientLight = new THREE.AmbientLight(0xff0000, 1); // 0.1
        this.scene.add(ambientLight);


        // this.addTest();
        
        this.createPlane(R.GRASS);

        this.createTree(R.TREE1, 50, 50);
        this.createTree(R.TREE2, 40, 50, 100, 0, -100, 0);
        this.createTree(R.TREE3, 60, 60, -50, 0, -200, 0);
        this.createTree(R.TREE4, 160, 120, -150, 0, -150, 0);
        

    }

    createTree (img, width, height, x = 0, y = 0, z = 0, rotation = 0) {
        let textureTree = new THREE.TextureLoader().load( img );
        let spriteMaterial = new THREE.SpriteMaterial({
            rotation: Math.PI * rotation,
            map: textureTree //设置精灵纹理贴图
        });
        // 创建精灵模型对象
        let sprite = new THREE.Sprite(spriteMaterial);
        // 控制精灵大小,
        sprite.scale.set(width, height, 1);
        sprite.position.set(x, height / 2 + y, z);

        this.scene.add(sprite);
        return sprite;
    }

    // 地平面
    createPlane (img) {
        let floor = new THREE.TextureLoader().load(img);
        floor.wrapS = THREE.RepeatWrapping;
        floor.wrapT = THREE.RepeatWrapping;
        floor.repeat.set(100, 100);
        const geometry = new THREE.PlaneGeometry( 15000, 15000, 32 );
        const material = new THREE.MeshBasicMaterial( {color: 0xaaaaaa, map: floor, side: THREE.DoubleSide} );
        const plane = new THREE.Mesh( geometry, material );
        plane.receiveShadow = true;
        plane.rotation.x = Math.PI / 2;
        plane.position.y = -0;
        this.scene.add( plane );
    }

    // render
    update (delta) {
        // console.log(this.camera);
    }

}
// End Class World


export function createWorld (id) {
    let e = new World(id);
    createGeneral.call(this, id, e);
}